from rest_framework.views import APIView
from rest_framework.response import Response
from django.http import HttpResponse
from apps.api.models.user import User
from apps.api.common.gen_cookie import create_cookie


class TestAPIView(APIView):
    def get(self, request):
        return Response({"message": "hello", "headers": request.headers, "session": request.session})


class HostView(APIView):
    def get(self, request):
        print("接收到请求")
        data = [{"name": "主机1", "port": 22}, {"name": "主机2", "port": 22}]
        return Response(data)


class LoginView(APIView):
    def post(self, request):
        print("接收到请求")
        print(request.data)
        cookie = create_cookie(request.data['username'])

        userModel = User()
        res = userModel.login(request.data['username'], request.data['password'])
        print("res: ", res)
        data = None
        if res:
            data = {"status": "success", "token": cookie}
        if res == None:
            data = {"status": "failed", "token": None}

        return Response(data)


class TestView(APIView):
    def get(self, request):
        user = User()
        data = user.test()
        print(data)
        return Response(data)